<?php

// Hook in

add_action('wp_footer', 'auftragsbild_footer');
add_action('init', 'auftragsbild_init');
//add_action('woocommerce_after_order_notes', 'auftragsbild_checkout_field');

add_action('woocommerce_checkout_fields', 'auftragsbild_checkout_field2');

function auftragsbild_init() {
	$GLOBALS['order_image_upload'] = "order_image_upload";
}
function auftragsbild_footer() {
	?>
	<script type="text/javascript">
		var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera",
				versionSearch: "Version"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};
	BrowserDetect.init();
	</script>
	<?php
	
	if(is_page('kasse')) {
	// wp_enqueue_script('jquery');
	
	
	js_localize('order_info', array('order_image_upload' => $GLOBALS['order_image_upload']));
	?>

	<script src="<?php bloginfo('stylesheet_directory'); ?>/js/vendor/jq.wd.js"></script>
	<script src="<?php bloginfo('stylesheet_directory'); ?>/js/jq.ifrtrans.js"></script>
	<script src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery.fileupl.js"></script>
	<script type="text/javascript">
	(function($){
		$(function () {
			$('#' + order_info.order_image_upload + '_extend').fileupload({
				dataType: 'json',
				progress : function (e, data) {
					var progress = parseInt(data.loaded / data.total * 100, 10);
					$('div#thumbnail').html('Uploading ...  ' + progress + '%');
				},
				done: function (e, data) {
					$.each(data.result, function (index, file) {
						// console.info(file);
						$('#' + order_info.order_image_upload).val(file.url);
						$('div#thumbnail').html('<img src="' + file.thumbnail_url + '" />');
						//$('#' + order_info.order_image_upload + '_extend').remove();
					});
				}
			});
		});
	})(jQuery)
	</script>
	<?php
	}
	
	if(is_page('produkte')) {
	?>
		<script type="text/javascript">
			(function($){
				var curr_menu;
				var product_id;
				var vari_id;
				var vari_name;
				var action_url = "?add-to-cart=variation&product_id=";
				var cX = 0; var cY = 0; var rX = 0; var rY = 0;
				var browser_add_width = 0;
				
				$("a[id*=add_to_cart]").mouseenter(
					function (e) {
						console.info($(this));
						console.info(e);
						console.info($(this).attr('data-product_id'));
						
						product_id = $(this).attr('data-product_id');
						
						// Hide exist 
						$("ul[id*=list-size-]").css('display', 'none');
						
						curr_menu = $("ul#list-size-" + product_id);
						console.info(BrowserDetect.browser);
						
						if(BrowserDetect.browser == "Firefox") {
							browser_add_width = 200;
						}
						
						
						if(curr_menu) {
							$(curr_menu).css('display', 'block');
							$(curr_menu).css('position', 'absolute');
							$(curr_menu).css('z-index', '999');
							$(curr_menu).css('left', e.target.offsetLeft + e.target.offsetWidth + browser_add_width);
							$(curr_menu).css('top', e.target.offsetTop);
							
							$(curr_menu).mouseleave(function(){$(this).css('display','none');});
							
						}
						
					}
				).click(function(){return false;});
				
				$("ul[id*=list-size-] > li").click(function(e){
					
					item_obj = new Object();
					item_obj.variation_id	= $(this).attr('data-variation_id');
					item_obj.quantity		= 1;
					item_obj.attribute_size	= $(this).attr('data-variation_size');
					item_obj.product_id		= $(this).attr('data-product_id');
					
					console.info(item_obj);
					
					if($("form#cart_form").length != 0) {
						$("form#cart_form").remove();
					}
					cart_form = $('<form>').attr({
						id		: 	'cart_form',
						name	: 	'cart_form',
						'data-product_id' : 487,
						method 	: 	'post',
						action 	: 	action_url + item_obj.product_id,
						enctype : 	'multipart/form-data',
						'accept-charset' : 'UTF-8'
					}).appendTo('body');
					
					$('<input>').attr({
						type: 'hidden',
						id: 'variation_id',
						name: 'variation_id',
						value : item_obj.variation_id
					}).appendTo(cart_form);
					
					$('<input>').attr({
						type: 'hidden',
						id: 'quantity',
						name: 'quantity',
						value : item_obj.quantity
					}).appendTo(cart_form);
					
					$('<input>').attr({
						type: 'hidden',
						id: 'attribute_size',
						name: 'attribute_size',
						value : item_obj.attribute_size
					}).appendTo(cart_form);
					
					$('<input>').attr({
						type: 'hidden',
						id: 'product_id',
						name: 'product_id',
						value : item_obj.product_id
					}).appendTo(cart_form);
					
					$(cart_form).submit();
				})
			})(jQuery);
			

		</script>
	<?php
	
	}
	
	
}


function auftragsbild_checkout_field2( $fields ) {
	
	
	$fields['order'][$GLOBALS['order_image_upload']]  = array (
		'type'          => 'file',
        'class'         => array($GLOBALS['order_image_upload'] . ' form-row-wide'),
		'required'  	=> false,
        'label'         => __('Laden Sie ein Bild hoch.'),
        'placeholder'       => __('Picture')
	);
	
	
	$fields['billing']['billing_state']['required'] = false;
	$fields['billing']['billing_state']['type'] = 'hidden';
	//$fields['billing']['billing_address_1']['placeholder'] = '';
	
	$fields['shipping']['shipping_state']['required'] = false;
	$fields['shipping']['shipping_state']['type'] = 'hidden';
	
	
	foreach ($fields['billing'] as &$field) {
		// print_r('<pre>');
		$field['placeholder'] = '';
		//	print_r($field['placeholder']);
	}
	
	foreach ($fields['shipping'] as &$field) {
		$field['placeholder'] = '';
	}
	
	foreach ($fields['order'] as &$field) {
		$field['placeholder'] = '';
	}
	
	
	/*
	print_r ("<pre>");
	print_r(get_option('woocommerce_enable_order_comments'));
	print_r ($fields['order']);
	print_r ("</pre>");
	*/
	return $fields;
}

if ( ! function_exists( 'woocommerce_form_field' ) ) {
	add_filter('woocommerce_form_field_file', 'auftragsbild_woocommerce_form_field_text', 10, 3);
	function auftragsbild_woocommerce_form_field_text($value, $key, $args) {
		//print_r($key);
		//print_r($args);
		//if($arg['type'] == 'text' && $key == $GLOBALS['order_image_upload']) {
			$id_name = $key;
			$id_name_extend = $key . '_extend';
			
			$field = '<p class="form-row ' . implode( ' ', $args['class'] ) .'" id="' . $id_name_extend . '_field">
						<label for="' . $id_name_extend . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required  . '</label>';
			$field .= '<input data-url="' . get_bloginfo('stylesheet_directory') . '/server/php/" type="file" name="' . $id_name_extend . '" id="' . $id_name_extend . '" class="image_upload ' . implode( ' ', $args['class'] ) .'" />';
			
			$field .= '<input style="display : none;" type="text" name="' . $id_name . '" id="' . $id_name . '" class="image_upload ' . implode( ' ', $args['class'] ) .'" />';
			
			//$field .= '<input type="file" name="file_upload" id="file_upload" />';
			
			$field .= '</p>';
			$field .= '<div id="thumbnail"></div>' . $after;
		//}
		return $field;
	}
}


/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'auftragsbild_checkout_field_update_order_meta');
/**
 * Update the user meta with field value
 **/
add_action('woocommerce_checkout_update_user_meta', 'auftragsbild_checkout_field_update_user_meta');



function auftragsbild_checkout_field_update_user_meta( $user_id ) {
	if ($user_id && $_POST[$GLOBALS['order_image_upload']]) update_user_meta( $user_id, $GLOBALS['order_image_upload'], esc_attr($_POST[$GLOBALS['order_image_upload']]) );
}


function auftragsbild_checkout_field_update_order_meta( $order_id ) {
	if ($_POST[$GLOBALS['order_image_upload']]) update_post_meta( $order_id, $GLOBALS['order_image_upload'], esc_attr($_POST[$GLOBALS['order_image_upload']]));
}


//add_filter('woocommerce_in_cart_product_title','title_with_category', 10, 2);
//add_filter('woocommerce_order_product_title','title_with_category', 10, 2);

function title_with_category($title, $values){
	
	$cat = wp_get_object_terms($values['data']->post->ID, 'product_cat');
	if($cat) {
		return $cat[0]->name . '<br/>' . '<small>' . $title . '</small>';
	}
	return '<small>' . $title . '</small>';
}

add_filter('woocommerce_countries','auftragsbild_woocommerce_countries');

function auftragsbild_woocommerce_countries($countries) {
	$countries = array( 
					'DE' => __('Germany', 'woocommerce'),
					'AT' => __('Austria', 'woocommerce'),
					'CH' => __('Switzerland', 'woocommerce'),
					'AE' => __('Europe', 'woocommerce') ,
					'ZZ' => __('Rest der Welt', 'woocommerce') 
				);
	return $countries;
}

?>